package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import android.content.Intent;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.taptech.doufu.R;
import com.taptech.doufu.listener.NormalInterface;
import com.taptech.doufu.ui.activity.ChoiceRankActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BlockModel;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.view.choice.adapter.ChoiceViewPagerAdapter;
import com.taptech.doufu.ui.view.choice.listener.OnLayoutHeight;
import com.taptech.doufu.ui.view.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ChoiceTopicRankPagerView extends LinearLayout {
    private Context mContext;
    private TabLayout tlTop;
    private TTHomeViewPager mViewPager;
    private List mSweepListViews;
    private View layMore;
    private boolean showMore;
    private EventBean eventData;

    public ChoiceTopicRankPagerView(Context context) {
        super(context);
    }

    public ChoiceTopicRankPagerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicRankPagerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicRankPagerView(final Context context, List<BlockModel.Item> list, List<NovelModel> defaultNovelList,
                                    final List<DFHomeCategoryBean.Item> moreList, final int index, final NormalInterface.OnIntegerListener onIntegerListener, final EventBean eventData) {
        this(context);
        this.mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_rank_complex_pager_view, null);
        layMore = view.findViewById(R.id.layMore);
        tlTop = view.findViewById(R.id.tlTop);
        tlTop.setTabMode(TabLayout.MODE_SCROLLABLE);
        this.eventData = eventData;
        mViewPager = view.findViewById(R.id.mViewPager);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,getLayoutHeight(defaultNovelList.size()));
        mViewPager.setLayoutParams(params);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                onIntegerListener.onResult(position);
                ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_TL_TAB,eventData);
                ((ChoiceTopicRankContentView)mSweepListViews.get(position)).loadData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (moreList != null && moreList.size() > index) {
            DFHomeCategoryBean.Item item = moreList.get(index);
            if (item != null && !TextUtils.isEmpty(item.getUrl())) {
                showMore = true;
            }
        }
        if (showMore) {
            layMore.setVisibility(View.VISIBLE);
        }else{
            layMore.setVisibility(View.GONE);
        }

        layMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                String data = JSON.toJSONString(moreList);
                intent.putExtra(Constant.DATA, data);
                intent.putExtra(Constant.TITLE, moreList.get(index).getTitle());
                intent.putExtra(Constant.PAGE_INDEX, index);
                intent.setClass(getContext(), ChoiceRankActivity.class);
                mContext.startActivity(intent);
            }
        });
        initViews(list,defaultNovelList,index==0?false:true);
        addView(view);
    }

    public void switchViewPager(int pos) {
        mViewPager.setCurrentItem(pos);
    }


    private int getLayoutHeight(int count) {
        return ScreenUtil.dip2px(mContext, (count/2+count%2)*84+10);
    }

    private HashMap<Integer,Integer> heightCache = new HashMap<>();




    private void initViews(List<BlockModel.Item> list, List<NovelModel> defaultNovelList,boolean needInit) {
        mSweepListViews = new ArrayList<>();
        List titles = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            BlockModel.Item model = list.get(i);
            titles.add(model.getTitle());
            List<NovelModel> initList;
            if (i == 0 && !needInit) {
                initList = defaultNovelList;
            }else{
                initList = new ArrayList<>();
            }
            final int index = i;
            ChoiceTopicRankContentView view = new ChoiceTopicRankContentView(mContext, initList,model, eventData, new OnLayoutHeight() {
                @Override
                public void onResult(int height) {
                    heightCache.put(index, height);
                }
            });
            mSweepListViews.add(view);
        }
        ChoiceViewPagerAdapter viewPagerAdapter = new ChoiceViewPagerAdapter(mSweepListViews,titles);
        mViewPager.setAdapter(viewPagerAdapter);
        tlTop.setupWithViewPager(mViewPager);


    }
}
